package com.chaoxing.v2;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) {
		// 书名为：佛陀的智慧,sourceId为6b5c8c8704a1409ba45479afbcb7b9df_4
		String sourceId = "6b5c8c8704a1409ba45479afbcb7b9df_4";
		// 获取图书相关信息
		JSONObject bookInfo = getBookInfo(sourceId + "&tradeId");
		System.out.println(bookInfo);

		// 转换为java对象
		Book book = JSONObject.toJavaObject(bookInfo, Book.class);

		// 输出书名
		System.out.println(book.getTitle());

		// 输出简介
		System.out.println(book.getShareDescription());

		// 获取章节信息
		LinkedList<Portion> portions = book.getPortions();
		String content = "";
		for (Portion portion : portions) {
			// 如果一个章节，没有父级信息，那么默认它就是书卷信息，即第几卷
			if (portion.getParentTitle() == null) {
				content = getChapterContent(sourceId, portion.getId(), portion.getBigContentId());
				portion.setChapterContent(content);
				continue;
			}

			// 获取并设置正文
			content = getContent(sourceId, portion.getId(), portion.getBigContentId());
			portion.setContent(content);
		}

		// 输出这本书的章节信息和正文
		for (Portion portion : portions) {
			if (portion.getChapterContent() != null) {
				System.out.println(portion.getChapterContent());
			}
			if (portion.getTitle() != null) {
				System.out.println(portion.getTitle());
			}
			if (portion.getContent() != null) {
				System.out.println(portion.getContent());
			}
		}
	}

	/**
	 * 获取包含html元素的卷名信息
	 * 
	 * @param sourceId
	 * @param id
	 * @param bigContentId
	 * @return
	 */
	public static String getChapterContent(String sourceId, String id, String bigContentId) {
		String baseUrlChapterContent = "http://yuedu.163.com/getChapterContent.do?sourceUuid=${sourceId}&articleUuid=${id}&bigContentId=${bigContentId}";
		String realUrl = baseUrlChapterContent.replace("${sourceId}", sourceId).replace("${id}", id)
				.replace("${bigContentId}", bigContentId);
		String rs = readUrl(realUrl);
		rs = JSONObject.parseObject(rs).getString("content");
		return new String(Base64.decodeBase64(rs));
	}

	/**
	 * 获取包含html元素的正文信息
	 * 
	 * @param sourceId
	 * @param id
	 * @param bigContentId
	 * @return
	 */
	public static String getContent(String sourceId, String id, String bigContentId) {
		String baseUrlContent = "http://yuedu.163.com/getArticleContent.do?sourceUuid=${sourceId}&articleUuid=${id}&bigContentId=${bigContentId}";
		String realUrl = baseUrlContent.replace("${sourceId}", sourceId).replace("${id}", id).replace("${bigContentId}",
				bigContentId);
		String rs = readUrl(realUrl);
		rs = JSONObject.parseObject(rs).getString("content");
		return new String(Base64.decodeBase64(rs));
	}

	/**
	 * 获取图书信息，包含书封，书名，简介，作者，章节信息等
	 * 
	 * @param wangyiBookId
	 * @return
	 */
	public static JSONObject getBookInfo(String wangyiBookId) {
		String baseUrl = "http://yuedu.163.com/getBook.do?id=${wangyiBookId}&tradeId=";
		String realUrl = baseUrl.replace("${wangyiBookId}", wangyiBookId);
		String urlResult = readUrl(realUrl);
		JSONObject rs = JSONObject.parseObject(urlResult);
		return rs;
	}

	/**
	 * 发起网络请求的方法
	 * 
	 * @param url
	 * @return
	 */
	public static String readUrl(String url) {
		try {
			List<String> lines = IOUtils.readLines(new URL(url).openStream(), "utf-8");
			String s = "";
			for (String line : lines) {
				s = s.concat(line);
			}
			return s;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}
